456D - A Lot of Games - CodeForces Solution


dp games strings *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pi pair<int,ll>
#define fi first
#define se second
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
const int N=2e5+5;
const int mod=1e9+7;

int trie[100005][26];
int now=1;
void add(string s){
    int i=0,nxt=0;
    while(i<s.size()){
        int c=s[i]-'a';
        if(trie[nxt][c]!=-1){
            nxt=trie[nxt][c];
        }else{
            nxt=trie[nxt][c]=now++;
            //cout<<nxt<<' '<<c<<' '<<trie[nxt][c]<<endl;
        }
        i++;
    }
}
bool dp1[100005],dp2[100005];

bool rec1(int x){
    bool ret=0;
    for(int i=0;i<26;i++){
        if(trie[x][i]!=-1){
            ret|=(!rec1(trie[x][i]));
        }
    }
    return dp1[x]=ret;
}
bool ch(int x){
    for(int i=0;i<26;i++){
        if(trie[x][i]!=-1)return false;
    }
    return true;
}
bool rec2(int x){
    bool ret=0;
    if(ch(x))return dp2[x]=1;
    for(int i=0;i<26;i++){
        if(trie[x][i]!=-1){
            ret|=(!rec2(trie[x][i]));
        }
    }
    return dp2[x]=ret;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n,k;cin>>n>>k;
    memset(trie,-1,sizeof trie);
    for(int i=0;i<n;i++){
        string s;cin>>s;
        add(s);
    }
    rec1(0);
    rec2(0);

    if(dp1[0]&&dp2[0]){
        cout<<"First"<<endl;
    }else if(!dp1[0]){
        cout<<"Second"<<endl;
    }else{
        if(k&1){
            cout<<"First"<<endl;
        }else{
            cout<<"Second"<<endl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe
1674A - Number Transformation
1244E - Minimizing Difference
1688A - Cirno's Perfect Bitmasks Classroom
219A - k-String
952A - Quirky Quantifiers
451B - Sort the Array
1505H - L BREAK into program
171E - MYSTERIOUS LANGUAGE
630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC
466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers